home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / libkcal / recurrencerule.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  13.3 KB  |  344 lines

  1. /*
  2.     This file is part of libkcal.
  3.  
  4.     Copyright (c) 1998 Preston Brown <pbrown@kde.org>
  5.     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
  6.     Copyright (c) 2002 David Jarvie <software@astrojar.org.uk>
  7.     Copyright (c) 2005, Reinhold Kainhofer <reinhold@kainhofer.com>
  8.  
  9.     This library is free software; you can redistribute it and/or
  10.     modify it under the terms of the GNU Library General Public
  11.     License as published by the Free Software Foundation; either
  12.     version 2 of the License, or (at your option) any later version.
  13.  
  14.     This library is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.     Library General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU Library General Public License
  20.     along with this library; see the file COPYING.LIB.  If not, write to
  21.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22.     Boston, MA 02110-1301, USA.
  23. */
  24. #ifndef KCAL_RECURRENCERULE_H
  25. #define KCAL_RECURRENCERULE_H
  26.  
  27. #include <qdatetime.h>
  28. #include <libkcal/listbase.h>
  29.  
  30. #include "libkcal_export.h"
  31.  
  32. template <class T>
  33. Q_INLINE_TEMPLATES void qSortUnique( QValueList<T> &lst )
  34. {
  35.   qHeapSort( lst );
  36.   if ( lst.isEmpty() ) return;
  37.   // Remove all duplicates from the times list
  38.   // TODO: Make this more efficient!
  39.   QValueListIterator<T> it = lst.begin();
  40.   T last = *it;
  41.   ++it;
  42.   T newlast;
  43.   while ( it != lst.end() ) {
  44.     newlast = (*it);
  45.     if ( newlast == last ) it = lst.remove( it );
  46.     else {
  47.       last = newlast;
  48.       ++it;
  49.     }
  50.   }
  51. }
  52.  
  53.  
  54. namespace KCal {
  55.  
  56. typedef QValueList<QDateTime> DateTimeList;
  57. typedef QValueList<QDate> DateList;
  58. typedef QValueList<QTime> TimeList;
  59.  
  60.  
  61.  
  62.  
  63. /**
  64.   This class represents a recurrence rule for a calendar incidence.
  65. */
  66. class LIBKCAL_EXPORT RecurrenceRule
  67. {
  68.   public:
  69.     class Observer {
  70.       public:
  71.         virtual ~Observer() {}
  72.         /** This method will be called on each change of the recurrence object */
  73.         virtual void recurrenceChanged( RecurrenceRule * ) = 0;
  74.     };
  75.     typedef ListBase<RecurrenceRule> List;
  76.     /** enum for describing the frequency how an event recurs, if at all. */
  77.     enum PeriodType { rNone = 0,
  78.            rSecondly, rMinutely, rHourly,
  79.            rDaily, rWeekly, rMonthly, rYearly
  80.          };
  81.     /** structure for describing the n-th weekday of the month/year. */
  82.     class WDayPos {
  83.     public:
  84.       WDayPos( int ps = 0 , short dy = 0 ) : mDay(dy), mPos(ps) {}
  85.       short day() const { return mDay; }
  86.       int pos() const { return mPos; }
  87.       void setDay( short dy ) { mDay = dy; }
  88.       void setPos( int ps ) { mPos = ps; }
  89.  
  90.       bool operator==( const RecurrenceRule::WDayPos &pos2 ) const {
  91.           return ( mDay == pos2.mDay ) && ( mPos == pos2.mPos );
  92.         }
  93.     protected:
  94.       short mDay;  // Weekday, 1=monday, 7=sunday
  95.       int mPos;    // week of the day (-1 for last, 1 for first, 0 for all weeks)
  96.                    // Bounded by -366 and +366, 0 means all weeks in that period
  97.     };
  98.  
  99.     RecurrenceRule( /*Incidence *parent, int compatVersion = 0*/ );
  100.     RecurrenceRule(const RecurrenceRule&);
  101.     ~RecurrenceRule();
  102.  
  103.     bool operator==( const RecurrenceRule& ) const;
  104.     bool operator!=( const RecurrenceRule& r ) const  { return !operator==(r); }
  105.     RecurrenceRule &operator=(const RecurrenceRule&);
  106.  
  107. //     Incidence *parent() const { return mParent; }
  108.  
  109.  
  110.     /** Set if recurrence is read-only or can be changed. */
  111.     void setReadOnly(bool readOnly) { mIsReadOnly = readOnly; }
  112.     /** Returns true if the recurrence is read-only, or false if it can be changed. */
  113.     bool isReadOnly() const  { return mIsReadOnly; }
  114.  
  115.  
  116.     /** Returns the event's recurrence status.  See the enumeration at the top
  117.      * of this file for possible values. */
  118.     bool doesRecur() const { return mPeriod!=rNone; }
  119.     void setRecurrenceType( PeriodType period );
  120.     PeriodType recurrenceType() const { return mPeriod; }
  121.     /** Turns off recurrence for the event. */
  122.     void clear();
  123.  
  124.  
  125.     /** Returns frequency of recurrence, in terms of the recurrence time period type. */
  126.     uint frequency() const { return mFrequency; }
  127.     /** Sets the frequency of recurrence, in terms of the recurrence time period type. */
  128.     void setFrequency( int freq );
  129.  
  130.  
  131.     /** Return the start of the recurrence */
  132.     QDateTime startDt() const   { return mDateStart; }
  133.     /** Set start of recurrence, as a date and time. */
  134.     void setStartDt(const QDateTime &start);
  135.  
  136.     /** Returns whether the start date has no time associated. Floating
  137.         means -- according to rfc2445 -- that the event has no time associate. */
  138.     bool doesFloat() const { return mFloating; }
  139.     /** Sets whether the dtstart is a floating time (i.e. has no time attached) */
  140.     void setFloats( bool floats );
  141.  
  142.  
  143.     /** Returns the date and time of the last recurrence.
  144.      * An invalid date is returned if the recurrence has no end.
  145.      * @param result if non-null, *result is updated to true if successful,
  146.      * or false if there is no recurrence.
  147.      */
  148.     QDateTime endDt( bool* result = 0 ) const;
  149.     /** Sets the date and time of the last recurrence.
  150.      * @param endDateTime the ending date/time after which to stop recurring. */
  151.     void setEndDt(const QDateTime &endDateTime);
  152.  
  153.  
  154.     /**
  155.      * Returns -1 if the event recurs infinitely, 0 if the end date is set,
  156.      * otherwise the total number of recurrences, including the initial occurrence.
  157.      */
  158.     int duration() const { return mDuration; }
  159.     /** Sets the total number of times the event is to occur, including both the
  160.      * first and last. */
  161.     void setDuration(int duration);
  162. //     /** Returns the number of recurrences up to and including the date specified. */
  163. //     int durationTo(const QDate &) const;
  164.     /** Returns the number of recurrences up to and including the date/time specified. */
  165.     int durationTo(const QDateTime &) const;
  166.     /** Returns the number of recurrences up to and including the date specified. */
  167.     int durationTo( const QDate &date ) const { return durationTo( QDateTime( date, QTime( 23, 59, 59 ) ) ); }
  168.  
  169.  
  170.  
  171.     /** Returns true if the date specified is one on which the event will
  172.      * recur. The start date returns true only if it actually matches the rule. */
  173.     bool recursOn( const QDate &qd ) const;
  174.     /** Returns true if the date/time specified is one at which the event will
  175.      * recur. Times are rounded down to the nearest minute to determine the result.
  176.      * The start date/time returns true only if it actually matches the rule. */
  177.     bool recursAt( const QDateTime & ) const;
  178.     /** Returns true if the date matches the rules. It does not necessarily
  179.         mean that this is an actual occurrence. In particular, the method does
  180.         not check if the date is after the end date, or if the frequency interval
  181.         matches */
  182.     bool dateMatchesRules( const QDateTime &qdt ) const;
  183.  
  184.  
  185.     /** Returns a list of the times on the specified date at which the
  186.      * recurrence will occur.
  187.      * @param date the date for which to find the recurrence times.
  188.      */
  189.     TimeList recurTimesOn( const QDate &date ) const;
  190.  
  191.  
  192.     /** Returns the date and time of the next recurrence, after the specified date/time.
  193.      * If the recurrence has no time, the next date after the specified date is returned.
  194.      * @param preDateTime the date/time after which to find the recurrence.
  195.      * @return date/time of next recurrence, or invalid date if none.
  196.      */
  197.     QDateTime getNextDate( const QDateTime& preDateTime ) const;
  198.     /** Returns the date and time of the last previous recurrence, before the specified date/time.
  199.      * If a time later than 00:00:00 is specified and the recurrence has no time, 00:00:00 on
  200.      * the specified date is returned if that date recurs.
  201.      * @param afterDateTime the date/time before which to find the recurrence.
  202.      * @return date/time of previous recurrence, or invalid date if none.
  203.      */
  204.     QDateTime getPreviousDate( const QDateTime& afterDateTime ) const;
  205.  
  206.  
  207.  
  208.  
  209.     void setBySeconds( const QValueList<int> bySeconds );
  210.     void setByMinutes( const QValueList<int> byMinutes );
  211.     void setByHours( const QValueList<int> byHours );
  212.  
  213.     void setByDays( const QValueList<WDayPos> byDays );
  214.     void setByMonthDays( const QValueList<int> byMonthDays );
  215.     void setByYearDays( const QValueList<int> byYearDays );
  216.     void setByWeekNumbers( const QValueList<int> byWeekNumbers );
  217.     void setByMonths( const QValueList<int> byMonths );
  218.     void setBySetPos( const QValueList<int> bySetPos );
  219.     void setWeekStart( short weekStart );
  220.  
  221.     const QValueList<int> &bySeconds() const { return mBySeconds; }
  222.     const QValueList<int> &byMinutes() const { return mByMinutes; }
  223.     const QValueList<int> &byHours() const { return mByHours; }
  224.  
  225.     const QValueList<WDayPos> &byDays() const { return mByDays; }
  226.     const QValueList<int> &byMonthDays() const { return mByMonthDays; }
  227.     const QValueList<int> &byYearDays() const { return mByYearDays; }
  228.     const QValueList<int> &byWeekNumbers() const { return mByWeekNumbers; }
  229.     const QValueList<int> &byMonths() const { return mByMonths; }
  230.     const QValueList<int> &bySetPos() const { return mBySetPos; }
  231.     short weekStart() const { return mWeekStart; }
  232.  
  233.  
  234.     void setDirty();
  235.     /**
  236.       Installs an observer. Whenever some setting of this recurrence
  237.       object is changed, the recurrenceUpdated( Recurrence* ) method
  238.       of each observer will be called to inform it of changes.
  239.       @param observer the Recurrence::Observer-derived object, which
  240.       will be installed as an observer of this object.
  241.     */
  242.     void addObserver( Observer *observer );
  243.     /**
  244.       Removes an observer that was added with addObserver. If the
  245.       given object was not an observer, it does nothing.
  246.       @param observer the Recurrence::Observer-derived object to
  247.       be removed from the list of observers of this object.
  248.     */
  249.     void removeObserver( Observer *observer );
  250.  
  251.     /**
  252.       Debug output.
  253.     */
  254.     void dump() const;
  255.     QString mRRule;
  256.  
  257.   private:
  258.     class Constraint {
  259.       public:
  260.         typedef QValueList<Constraint> List;
  261.  
  262.         Constraint( int wkst = 1 );
  263. /*         Constraint( const Constraint &con ) :
  264.                      year(con.year), month(con.month), day(con.day),
  265.                      hour(con.hour), minute(con.minute), second(con.second),
  266.                      weekday(con.weekday), weeknumber(con.weeknumber),
  267.                      yearday(con.yearday), weekstart(con.weekstart) {}*/
  268.         Constraint( const QDateTime &preDate, PeriodType type, int wkst );
  269.         void clear();
  270.  
  271.         int year;       // 0 means unspecified
  272.         int month;      // 0 means unspecified
  273.         int day;        // 0 means unspecified
  274.         int hour;       // -1 means unspecified
  275.         int minute;     // -1 means unspecified
  276.         int second;     // -1 means unspecified
  277.         int weekday;    //  0 means unspecified
  278.         int weekdaynr;  // index of weekday in month/year (0=unspecified)
  279.         int weeknumber; //  0 means unspecified
  280.         int yearday;    //  0 means unspecified
  281.         int weekstart;  //  first day of week (1=monday, 7=sunday, 0=unspec.)
  282.  
  283.         bool readDateTime( const QDateTime &preDate, PeriodType type );
  284.         bool matches( const QDate &dt, RecurrenceRule::PeriodType type ) const;
  285.         bool matches( const QDateTime &dt, RecurrenceRule::PeriodType type ) const;
  286.         bool isConsistent() const;
  287.         bool isConsistent( PeriodType period ) const;
  288.         bool increase( PeriodType type, int freq );
  289.         QDateTime intervalDateTime( PeriodType type ) const;
  290.         DateTimeList dateTimes( PeriodType type ) const;
  291.         void dump() const;
  292.     };
  293.  
  294.     Constraint getNextValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
  295.     Constraint getPreviousValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
  296.     DateTimeList datesForInterval( const Constraint &interval, PeriodType type ) const;
  297.     bool mergeIntervalConstraint( Constraint *merged, const Constraint &conit,
  298.                                   const Constraint &interval ) const;
  299.     bool buildCache() const;
  300.  
  301.  
  302.     PeriodType mPeriod;
  303.     QDateTime mDateStart;
  304.     /** how often it recurs (including dtstart):
  305.           -1 means infinitely,
  306.            0 means an explicit end date,
  307.            positive values give the number of occurrences */
  308.     int mDuration;
  309.     QDateTime mDateEnd;
  310.     uint mFrequency;
  311.  
  312.     bool mIsReadOnly;
  313.     bool mFloating;
  314.  
  315.     QValueList<int> mBySeconds;     // values: second 0-59
  316.     QValueList<int> mByMinutes;     // values: minute 0-59
  317.     QValueList<int> mByHours;       // values: hour 0-23
  318.  
  319.     QValueList<WDayPos> mByDays;   // n-th weekday of the month or year
  320.     QValueList<int> mByMonthDays;   // values: day -31 to -1 and 1-31
  321.     QValueList<int> mByYearDays;    // values: day -366 to -1 and 1-366
  322.     QValueList<int> mByWeekNumbers; // values: week -53 to -1 and 1-53
  323.     QValueList<int> mByMonths;      // values: month 1-12
  324.     QValueList<int> mBySetPos;      // values: position -366 to -1 and 1-366
  325.     short mWeekStart;               // first day of the week (1=Monday, 7=Sunday)
  326.  
  327.     Constraint::List mConstraints;
  328.     void buildConstraints();
  329.     bool mDirty;
  330.     QValueList<Observer*> mObservers;
  331.  
  332.     // Cache for duration
  333.     mutable DateTimeList mCachedDates;
  334.     mutable bool mCached;
  335.     mutable QDateTime mCachedDateEnd;
  336.  
  337.     class Private;
  338.     Private *d;
  339. };
  340.  
  341. }
  342.  
  343. #endif
  344.